package com.ddd.mill.infrastructure.persistent.repository.impl.product;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ddd.mill.domain.aggregate.product.entity.ProductDetail;
import com.ddd.mill.domain.aggregate.product.repository.ProductDetailRepository;
import com.ddd.mill.infrastructure.persistent.dao.product.ProductDetailDao;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @author liu
 * @title: ProductDetailMyBatisRepositoryImpl
 * @projectName trading-center
 * @date 2020/11/3010:24
 */
@Component
public class ProductDetailMyBatisRepositoryImpl implements ProductDetailRepository {
    @Resource
    ProductDetailDao dao;
    @Override
    public ProductDetail load(Long id) {
        return dao.selectById(id);
    }

    @Override
    public Long save(ProductDetail entity) {
        if (!Objects.isNull(entity.getId()) && !Objects.isNull(dao.selectById(entity.getId()))) {
            dao.updateById(entity);
        } else {
            dao.insert(entity);
        }
        return entity.getId();
    }


    @Override
    public ProductDetail getByProductId(Long id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("product_id", id);
        return dao.selectOne(queryWrapper);
    }
}
